package net.oschina.app.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;


import net.oschina.app.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/7/13.
 */

public class BaseAdapter extends FragmentStatePagerAdapter {
    private Context context;
    private List<Class<? extends BaseFragment>> clazzs = new ArrayList<>();
    private List<String> tabTitls = new ArrayList<>(); //页签的标题集合
    private List<Bundle> bundles = new ArrayList<>(); //要传给片段对象的bundle的集合

    public BaseAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    public void addTab(String title, Class<? extends BaseFragment> clz, Bundle bundle) {
        clazzs.add(clz);
        tabTitls.add(title);
        bundles.add(bundle);
        notifyDataSetChanged();
    }

    public void clear() {
        clazzs.clear();
        tabTitls.clear();
        bundles.clear();
        notifyDataSetChanged();
    }


    @Override
    public Fragment getItem(int position) {
        Fragment instantiate = Fragment.instantiate(context, clazzs.get(position).getName(), bundles.get(position));
        return instantiate;
    }

    @Override
    public int getCount() {
        return clazzs.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitls.get(position);
    }
}
